package cn.iocoder.yudao.module.train.dal.mysql.courseclasses;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.CourseClassesPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.VipClassesPageReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.VipClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.course.CourseDO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.VipClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursestudentregistform.CourseStudentRegistFormDO;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.module.train.dal.dataobject.venue.VenueDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 会员课程 Mapper
 *
 * @author waynelam
 */
@Mapper
public interface VipClassesMapper extends BaseMapperX<VipClassesDO> {

    default PageResult<VipClassesCommodityDTO> selectPage(VipClassesPageReqVO reqVO) {
        Page<VipClassesCommodityDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        MPJLambdaWrapper<VipClassesDO> wrapper = new MPJLambdaWrapper<VipClassesDO>()
                .selectAll(CourseClassesDO.class)
                .selectAs(CourseDO::getName, CourseClassesCommodityDTO::getCourseName)
                .select(TeacherDO::getTeacherName)
                .select(CourseCommodityDO::getCommodityName)
                .select(CourseCommodityDO::getBeginTime)
                .select(CourseCommodityDO::getEndTime)
                .eq(CourseCommodityDO::getVipFlag, 1)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                .selectSub(CourseStudentRegistFormDO.class,
                        w -> w
                                .selectCount(CourseStudentRegistFormDO::getId)
                                .eq(CourseStudentRegistFormDO::getClassesId, CourseClassesDO::getId),
                        CourseClassesCommodityDTO::getStudentQuantity)
                .likeIfExists(CourseDO::getName, reqVO.getCourseName())
                .likeIfExists(CourseCommodityDO::getCommodityName, reqVO.getCommodityName())
                .likeIfExists(TeacherDO::getTeacherName, reqVO.getTeacherName());
        IPage<VipClassesCommodityDTO> resultPage = selectJoinPage(page, VipClassesCommodityDTO.class, wrapper);
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

    default VipClassesCommodityDTO selectById(Long id) {
        return selectJoinOne(VipClassesCommodityDTO.class,
                new MPJLambdaWrapper<VipClassesDO>()
                        .selectAll(CourseClassesDO.class)
                        .selectAs(CourseDO::getName, CourseClassesCommodityDTO::getCourseName)
                        .select(TeacherDO::getTeacherName)
                        .select(CourseCommodityDO::getCommodityName)
                        .select(CourseCommodityDO::getBeginTime)
                        .select(CourseCommodityDO::getEndTime)
                        .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                        .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                        .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                        .eq(CourseClassesDO::getId, id));
    }

    default List<VipClassesCommodityDTO> selectList(VipClassesPageReqVO reqVO) {
        MPJLambdaWrapper<VipClassesDO> wrapper = new MPJLambdaWrapper<VipClassesDO>()
                .selectAll(CourseClassesDO.class)
                .selectAs(CourseDO::getName, CourseClassesCommodityDTO::getCourseName)
                .select(TeacherDO::getTeacherName)
                .select(CourseCommodityDO::getCommodityName)
                .select(CourseCommodityDO::getBeginTime)
                .select(CourseCommodityDO::getEndTime)
                .eq(CourseCommodityDO::getVipFlag, 1)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                .likeIfExists(CourseDO::getName, reqVO.getCourseName())
                .likeIfExists(CourseCommodityDO::getCommodityName, reqVO.getCommodityName())
                .likeIfExists(TeacherDO::getTeacherName, reqVO.getTeacherName());

        return selectJoinList(VipClassesCommodityDTO.class, wrapper);
    }

    default VipClassesDO selectByCommodityId(Long commodityId) {
        return selectOne(new LambdaQueryWrapperX<VipClassesDO>()
                .eq(VipClassesDO::getCommodityId, commodityId)
        );
    }

}
